<?php
//引入数据库类
require_once dirname(__FILE__).'/../dao/MysqlDb.class.php';
/*
 * 学生类(对student_info表操作)
 */
class StudentModel{
	private $mySqlDb = null;
	//测试
	private $TAG = "StudentModel";
	
	//登录(使用账号查找匹配的密码,对比密码,1.密码正确->返回true。2.密码不正确->返回提示(账号不存在或者密码不正确)。3.密码正确->返回true)
 	public function login($studentId, $studentPassword){
		//获取数据库实例
		$mySqlDb= MySqlDb::getMySqlDb();
		//数据转义
		$mySqlDb->escapeString($studentId);
		$mySqlDb->escapeString($studentPassword);
		//sql(查出与之对应密码)
		$sql = "select student_password from student_info where student_id='$studentId';";
		//取密码
		$password = $mySqlDb->fetchRow($sql);
		//数据为空
		if($password == false){
			//返回错误信息(账号不存在!)
			return $errStr = "账号不存在!";
		} else if($password['student_password'] !== $studentPassword) {	
			//var_dump($password);
			//返回错误信息(密码不正确!)
			return $errStr = "密码不正确!";
		} else{
			//成功验证
			return true;
		}
		
	}
	
	//显示已选课程(根据学号查找student_courses和courses表,返回选修课程编号,课程名,任课老师以及当前人数)
	public function electiveCourse($studentId){
		//获取数据库实例
		$mySqlDb = MySqlDb::getMySqlDb();
		//数据转义
		$mySqlDb->escapeString($studentId);
		
		$sql = "select courses.course_id,course_name,course_student_count_current,course_teacher_name from student_courses,
			courses where courses.course_id=student_courses.course_id and student_id='$studentId'";
		$electiveCourseInfo = $mySqlDb->fetchAll($sql);
		//数据为空
		if($electiveCourseInfo == null){
			return false;
		} else{
			//返回已选课程
			return $electiveCourseInfo;
		}
	}
	
	//退选课程(根据courseId和studentId退选)
	public function dropCourse($courseId, $studentId){
		//获取数据库实例
		$mySqlDb = MySqlDb::getMySqlDb();
		//数据转义
		$mySqlDb->escapeString($courseId);
		$mySqlDb->escapeString($studentId);
		
		$sql = "delete from student_courses where `course_id`='$courseId' and `student_id`='$studentId';";
		//执行删除
		$mySqlDb->query($sql);
		//退选之后courses中当前课程人数-1
		$sql = "update courses set `course_student_count_current`=`course_student_count_current`-1 where `course_id`='$courseId';";
		$mySqlDb->query($sql);
	}
	
	//显示可选课程(只显示1.人数未满以及2.该学生未选修的课程,对courses表进行操作)
	public function canSelctCourses($studentId){
		//获取数据库实例
		$mySqlDb = MySqlDb::getMySqlDb();
		//数据转义
		$mySqlDb->escapeString($studentId);
		$sql = "select * from courses where (`course_student_count_max` > `course_student_count_current` and `course_id` not in(select `course_id` from student_courses where `student_id`='$studentId'));";
		$canSelectCourseInfo = $mySqlDb->fetchAll($sql);
		//数据为空
		if($canSelectCourseInfo == null){
			return false;
		} else {
			//返回课程信息
			return $canSelectCourseInfo;
		}
	}
	
	//参加选课(对student_courses表进行操作(插入数据))
	public function addElectiveCourse($studentId, $courseId){
		//获取数据库实例
		$mySqlDb = MySqlDb::getMySqlDb();
		//数据转义
		$mySqlDb->escapeString($studentId);
		$mySqlDb->escapeString($courseId);
		
		//添加选课信息
		$sql = "insert into student_courses values(null, '$studentId', '$courseId')";
		$mySqlDb->query($sql);
		//课程表中的当前人数+1
		$sql = "update courses set `course_student_count_current`=`course_student_count_current`+1 where `course_id`='$courseId';";
		$mySqlDb->query($sql);
	}

} 


//单元测试
//$studentModel = new StudentModel();

//登录测试
/* $account = "14408100105";
$password = "lanxuewei";
$login = $studentModel->login($account, $password);
echo "登录信息为：".$login; */

//显示所选课程测试
/* $allCourses = $studentModel->electiveCourse('14408100105');
echo "所有选课信息为：".'<br/>';
foreach ($allCourses as $row){
	echo "课程编号：".$row['course_id'].'&nbsp&nbsp';
	echo "课程名称：".$row['course_name'].'&nbsp&nbsp';
	echo "任课老师：".$row['course_teacher_name'].'&nbsp&nbsp';
	echo "当前人数".$row['course_student_count_current'].'<br/>';
} */

//显示可选课程测试
/* $allCanSelectCourseInfo = $studentModel->canSelctCourses('14408100105');
echo "所有可选课程信息为：".'</br>';
foreach ($allCanSelectCourseInfo as $row){
	echo "课程编号：".$row['course_id'].'&nbsp&nbsp';
	echo "课程名称：".$row['course_name'].'&nbsp&nbsp';
	echo "任课老师：".$row['course_teacher_name'].'&nbsp&nbsp';
	echo "当前人数".$row['course_student_count_current'].'<br/>';
} */


?>